package com.cyq.day06;

import java.util.Scanner;

public class HomeWork02 {
    public static class ATM{
        private  static String[][] sysUsers = {
                {"aaaaaa", "111111", "1000"},
                {"bbbbbb", "222222", "2000"},
                {"cccccc", "333333", "3000"},
                {"dddddd", "444444", "4000"},
                {"eeeeee", "555555", "5000"}
        };
    }
    public static void main(String[] args) {
        login();
        //ATM机：多用户版本
        //登录系统：系统存储5个用户的账号密码余额
        //选择菜单：存，取，查，转账，退出
        //如果三次输入错误则系统退出
        //如果登录成功，显示菜单
    }
        public static void login(){
             int count = 0;
            for (int i = 1; i <= 3; i++) {
                System.out.println("请输入账号:");// 提示用户输入账户
                Scanner userSc = new Scanner(System.in);
                String userIn = userSc.next();
                System.out.println("请输入密码:");// 提示用户输入密码
                Scanner passwordSc = new Scanner(System.in);
                String passwordIn = passwordSc.next();
                for (int j = 0; j <  ATM.sysUsers.length; j++) {// 判断输入账户和密码是否匹配数据库
                    if ( ATM.sysUsers[j][0].equals(userIn) && ATM.sysUsers[j][1].equals(passwordIn)) {
                        count = j;// 匹配成功，将当前账户在数据库中的账户组的下标值赋值给count
                        welcome(count);
                    } else if (i == 1 && j == ATM.sysUsers.length - 1) {// 第一次输入错误
                        System.out.println("账号或密码错误，请重新输入！");
                    } else if (i == 2 && j == ATM.sysUsers.length - 1) {// 第二次输入错误
                        System.out.println("账号或密码错误，请重新输入！");
                        System.out.println("这是您的最后一次输入机会，若账号或密码错误，将锁定您的银行卡");
                    } else if (j == ATM.sysUsers.length - 1) {// 第三次输入错误
                        System.out.println("您的错误次数超过限制，请联系相关银行！");
                        System.exit(0);// 三次错误后，程序终止
                    }
                }
            }
        }
        public static void welcome(int count){
            Scanner scanner = new Scanner(System.in);
            while (true) {
                int wrongFlag2 = 0;
                System.out.println("请选择：1-存款 2-取款 3-查询 4-转账 5-退出 6-切换账号");
                int input = scanner.nextInt();
                switch (input) {
                    case 1:
                        System.out.println("请输入想要存款的金额");
                        int deposit = scanner.nextInt();
                        ATM.sysUsers[count][2] = Double.parseDouble( ATM.sysUsers[count][2]) + deposit + "";
                        System.out.println("账户的余额为" +  ATM.sysUsers[count][2]);
                        break;
                    case 2:
                        System.out.println("请输入想要取出的金额");
                        int withDraw = scanner.nextInt();
                        if (Double.parseDouble(ATM.sysUsers[count][2])>=withDraw) {
                            ATM.sysUsers[count][2] = Double.parseDouble(ATM.sysUsers[count][2]) - withDraw + "";
                            System.out.println("账户的余额为" + ATM.sysUsers[count][2]);
                        }else {
                            System.out.println("余额不足");
                        }
                        break;
                    case 3:
                        System.out.println("查询账户的余额为" +  ATM.sysUsers[count][2]);
                        break;
                    case 4:
                        boolean isExist=false;
                        System.out.println("请输入想要转账的账户的用户名");
                        String userId = scanner.next();
                        if (userId.equals(ATM.sysUsers[count][0])){
                            System.out.println("不能转给自己");
                            continue;
                        }
                        int tranSportID = 0;
                        for (int i = 0; i <  ATM.sysUsers.length; i++) {
                            if (userId.equals( ATM.sysUsers[i][0])) {
                                tranSportID = i;
                                isExist=true;
                            }
                        }
                        if (!isExist){
                            System.out.println("你输入的账号不存在请重新输入");
                            continue;
                        }
                        System.out.println("请输入想要转入的金额");
                        double tranSport = scanner.nextDouble();
                        if (tranSport>=Double.parseDouble(ATM.sysUsers[count][2])){
                            System.out.println("余额不足不能转账");
                            continue;
                        }
                        ATM.sysUsers[count][2] = Double.parseDouble( ATM.sysUsers[count][2]) - tranSport + "";
                        ATM.sysUsers[tranSportID][2] = Double.parseDouble( ATM.sysUsers[tranSportID][2]) + tranSport + "";
                        System.out.println("账户的余额为" +  ATM.sysUsers[count][2]);
                        System.out.println("转入账户的余额为" +  ATM.sysUsers[tranSportID][2]);
                        break;
                    case 5:
                        System.out.println("请取走你的卡");
                        System.exit(0);
                    case 6:
                        login();
                    default:
                        System.out.println("输入有误");
                        wrongFlag2++;
                        break;
                }
                if (wrongFlag2 >= 3) {
                    System.out.println("输入错误三次系统自动退出");
                    System.exit(0);
                }
            }
        }
}
